Skip to content

连接

Node.js 连接 MySQL

依赖包

js
npm install mysql2

测试用例

js
// get the client
const mysql = require("mysql2");

// create the connection to database
const connection = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "password",
  database: "database",
});

// simple query
connection.query(
  'SELECT * FROM `table` WHERE `name` = "Page" AND `age` > 45',
  function (err, results, fields) {
    console.log(results); // results contains rows returned by server
    console.log(fields); // fields contains extra meta data about results, if available
  }
);

参考链接

https://www.npmjs.com/package/mysql2